6550 #include "types.h"
6551 #include "x86.h"
6552 #include "defs.h"
6553 #include "kbd.h"
6554 
6555 int
6556 kbdgetc(void)
6557 {
6558   static uint shift;
6559   static uchar *charcode[4] = {
6560     normalmap, shiftmap, ctlmap, ctlmap
6561   };
6562   uint st, data, c;
6563 
6564   st = inb(KBSTATP);
6565   if((st & KBS_DIB) == 0)
6566     return -1;
6567   data = inb(KBDATAP);
6568 
6569   if(data == 0xE0){
6570     shift |= E0ESC;
6571     return 0;
6572   } else if(data & 0x80){
6573     // Key released
6574     data = (shift & E0ESC ? data : data & 0x7F);
6575     shift &= ~(shiftcode[data] | E0ESC);
6576     return 0;
6577   } else if(shift & E0ESC){
6578     // Last character was an E0 escape; or with 0x80
6579     data |= 0x80;
6580     shift &= ~E0ESC;
6581   }
6582 
6583   shift |= shiftcode[data];
6584   shift ^= togglecode[data];
6585   c = charcode[shift & (CTL | SHIFT)][data];
6586   if(shift & CAPSLOCK){
6587     if('a' <= c && c <= 'z')
6588       c += 'A' - 'a';
6589     else if('A' <= c && c <= 'Z')
6590       c += 'a' - 'A';
6591   }
6592   return c;
6593 }
6594 
6595 void
6596 kbdintr(void)
6597 {
6598   consoleintr(kbdgetc);
6599 }
